//
//  JLInitialize.m
//  JLStyleApp
//
//  Created by LvJianfeng on 15/8/19.
//  Copyright (c) 2015年 JLStyle. All rights reserved.
//

#import "JLInitialize.h"
@interface JLInitialize()
@end

@implementation JLInitialize

+ (void)initDB{
    FMDatabase *db = nil;
    NSError *error;
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:[self databaseCityFilePath]] == NO) {
        //不存在数据库文件，复制去
        NSString *localDBPath = [[NSBundle mainBundle] pathForResource:@"dbcity" ofType:@"sqlite"];
        BOOL isOK = [fileManager copyItemAtPath:localDBPath toPath:[self databaseCityFilePath] error:&error];
        if (!isOK) {
            NSLog(@"initDB:%@",error);
        }
    }
    
    if ([fileManager fileExistsAtPath:[self databaseFilePath]] == NO) {
        //不存在数据库文件，需创建
        db = [self createDB];
    }
}

//得到存放数据的地址
+ (NSString *)databaseFilePath
{
    NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentPath = [filePath objectAtIndex:0];
	NSLog(@"%@",filePath);
	NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"dbjlstyle.sqlite"];
	return dbFilePath;
}

+ (NSString *)databaseCityFilePath
{
    NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [filePath objectAtIndex:0];
    NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"dbcity.sqlite"];
    return dbFilePath;
}

//创建数据库
+ (FMDatabase *)createDB{
    FMDatabase *db = [FMDatabase databaseWithPath:[self databaseFilePath]];
    if (db) {
        [db open];
        //为数据库设置缓存，提高查询效率
        [db setShouldCacheStatements:YES];
        //创建表
        [self createTable];
        return db;
    }
    return nil;
}

//创建数据表
+ (void)createTable{
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self databaseFilePath]];
    [queue inDatabase:^(FMDatabase *db) {
        NSLog(@"--->Create Table ...");
        //证书展示表
        BOOL isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS JL_Certificate (jid INTEGER PRIMARY KEY AUTOINCREMENT,cname TEXT,cimage TEXT,cdescr TEXT)"];
        isCreate?NSLog(@"--->SUCCESS:JL_Certificate(证书展示表)"):NSLog(@"--->ERROR:JL_Certificate(证书展示表)");
        
        //教育经历表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_EduExperience` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`schoolName` text,`domain` text,`diplomas` text,`finishSchoolTime` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_EduExperience(教育经历表)"):NSLog(@"--->ERROR:JL_EduExperience(教育经历表)");
        
        //期望工作表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_ExpectWork` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`position` text,`quality` INTEGER NOT NULL,`city` text,`compensation` INTEGER,`remark` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_ExpectWork(期望工作表)"):NSLog(@"--->ERROR:JL_ExpectWork(期望工作表)");
        
        //项目经验表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_ProjectExperience` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`projectName` text,`responsibility` text,`projectStartTime` INTEGER,`projectEndTime` INTEGER,`projectExplain` text,`projectUrl` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_ProjectExperience(项目经验表)"):NSLog(@"--->ERROR:JL_ProjectExperience(项目经验表)");
        
        //技能表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_Skill` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`skillName` varchar(50),`skillLevel` INTEGER)"];
        isCreate?NSLog(@"--->SUCCESS:JL_Skill(技能表)"):NSLog(@"--->ERROR:JL_Skill(技能表)");
        
        //用户表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_User` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`phone` text NOT NULL,`password` text NOT NULL,`token` text,`createTime` INTEGER)"];
        isCreate?NSLog(@"--->SUCCESS:JL_User(用户表)"):NSLog(@"--->ERROR:JL_User(用户表)");
        
        //自我描述
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_UserDescr` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`descr` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_UserDescr(自我描述)"):NSLog(@"--->ERROR:JL_UserDescr(自我描述)");
        
        //用户信息表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_UserWorkInfo` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`monthMoney` text,`business` text,`company` text,`school` text,`currentJob` text,`expectJob` text,`workYears` INTEGER)"];
        isCreate?NSLog(@"--->SUCCESS:JL_UserInfo(用户信息表)"):NSLog(@"--->ERROR:JL_UserInfo(用户信息表)");
        
        //用户工作信息表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_UserInfo` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`sex` INTEGER NOT NULL,`avatar` text,`email` text,`birthday` text,`city` text,`diplomas` text,`phone` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_UserInfo(用户工作信息表)"):NSLog(@"--->ERROR:JL_UserInfo(用户工作信息表)");
        
        //工作经历表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_WorkExperience` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`companyName` text,`companyLogo` text,`currentJob` text,`workStartTime` text,`workEndTime` text,`workContent` text,`remark` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_WorkExperience(工作经历表)"):NSLog(@"--->ERROR:JL_WorkExperience(工作经历表)");
        
        //作品展示表
        isCreate = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS `JL_WorksShow` (`jid` INTEGER PRIMARY KEY AUTOINCREMENT,`uid` INTEGER NOT NULL,`type` INTEGER NOT NULL,`worksUrl` text,`worksDescr` text,`worksTitle` text,`worksImages` text)"];
        isCreate?NSLog(@"--->SUCCESS:JL_WorksShow(作品展示表)"):NSLog(@"--->ERROR:JL_WorksShow(作品展示表)");
        
        NSLog(@"<---End Create");
    }];
}
@end
